//
//  InputValidation.swift
//  FoundationCustom
//
//  Created by ZhouJiatao on 2018/10/26.
//  Copyright © 2018 Unknow. All rights reserved.
//

import Foundation




/// 检查用户输入合法性
public struct InputValidation {
    
    public typealias ErrorMsg = String
    
    /// 检查手机号合法性
    public static func phone(_ input: String?) -> ErrorMsg? {
        
        let minPhoneCount = 11
        let maxPhoneCount = 12

        let phone = input ?? ""
        
        if maxPhoneCount < phone.count || minPhoneCount > phone.count {
            return CBLocalizedString(.input_hint$incorrect_phone, comment: "")
        }
        
        return nil
    }
    
    public static func email(_ input: String?) -> ErrorMsg? {
        
        let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
        
        let predicate = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
        
        if predicate.evaluate(with: input) {
            return nil
        }
        
        return "请输入正确的邮箱" //TODO: 本地化
    }
    
}
